using System;

namespace Handrise.BasicModal.Serialization
{
    /// <summary>
    /// All possible platform under which CFormatter could be launched.
    /// <remarks>Really speaking actually i'm just interested in differentiate 
    /// between CompactFramework and other Frameworks</remarks>
    /// </summary>
    public enum FrameworkVersion { NETCF10, NET10, NET11, MONO, DOTGNU, SSCLI, UNKNOWN }
    /// <summary>
    /// This class is used to detect Framework actually running under CFormatter.
    /// </summary>
    public class Framework
    {

        /// <summary>
        /// A single static method is the core of Framework class.
        /// This Detect method is used to detect which implementation is being used
        /// by CompactFormatter
        /// </summary>
        /// <returns>A FrameworkVersion enum representing the implementation used
        /// </returns>
        public static FrameworkVersion Detect()
        {
            OperatingSystem os = Environment.OSVersion;

            /* This is uncorrect on the long run:
             * Actually Platform is simply telling us that the OS is Unix but
             * this value (128) is defined only under Mono.
             * In fact this code will not detect Mono under windows and, if dotGNU
             * uses the same code (128), this code will also uncorrectly identify
             * dotGNU as MONO.
             * As already said this is ok because we're particularly interested 
             * in differentiate between NETCF and other implementations.
             * */
            if ((int)os.Platform == 128)
                return FrameworkVersion.MONO;

            if (os.Platform == PlatformID.WinCE && os.Version.Revision == -1)
                return FrameworkVersion.NETCF10;

            if (Environment.Version.Major == 1)
            {
                if (Environment.Version.Minor == 1)
                    return FrameworkVersion.NET11;
                else if (Environment.Version.Minor == 0)
                    return FrameworkVersion.NET10;
            }



            return FrameworkVersion.UNKNOWN;
        }
    }
}
